package code.structure.manager.pop 
{
	import code.main.App;
	import code.structure.manager.pop.view.Windows;
	import code.structure.module.others.ProjectAssistController;
	import code.structure.module.others.SpecialController;
	import code.tools.bitmap.FrameDrive;
	import code.tools.commponents.Alignment;
	import code.tools.number.SortVector;
	import code.untils.SceneConfig;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	/**
	 * 弹窗控制类
	 * @author CYang
	 */
	public class WindowManager
	{
		/*弹窗背景*/
		private var _mBackGround:Sprite;
		//弹出窗口队列
		private var _aWindowsQueue:Vector.<Windows>;
		//弹窗字典 用于存储生成的弹窗实例
		private var _diction:Dictionary;
		
		private var _controller:ProjectAssistController;
		//窗体容器
		private var _mMain:Sprite;
		
		/**
		 * 是否有窗口正在关闭
		 */		
		public var isClosing:Boolean;
		
		public function WindowManager(c:ProjectAssistController) 
		{
			//_controller.scene.isoWorld.status;
			_controller= c;
		}
		/**
		 * 初始化方法
		 */
		private function init():void
		{
			_mBackGround = new Sprite();
			_mBackGround.visible = false;
			_mMain.addChild(_mBackGround);
			App.appstage.addEventListener(Event.RESIZE,drawBackGround);
			drawBackGround();
			_diction = new Dictionary();
			_aWindowsQueue = new Vector.<Windows>();
		}
		/**
		 * 是否有弹窗在显示中
		 */		
		public function get isWindowInQueue():Boolean{
			return _aWindowsQueue.length > 0;
		}
		/**
		 * 显示弹窗
		 * @param	win  窗体
		 * @param	showNow 是否立马显示
		 * @param	priority 弹窗优先级
		 * @param	showBg 弹窗是否显示背景
		 * @param	backPrev 是否把之前显示的窗压回队列
		 */
		public function showWindow(win:Windows, showNow:Boolean = false, priority:int = 0, showBg:Boolean = true, backPrev:Boolean = false, pause:Boolean= true):void
		{	
			if(win) {
				win.priority = priority;
				win.bgVisible = showBg;
				setPoint(win);
				
				if (!showNow)
					_aWindowsQueue.push(win);	//不是立刻显示就加到队列尾部
				else
					_aWindowsQueue.unshift(win);//如果是立刻显示就添加到队首
			}
				
			if (backPrev) backAllWindows();	//回退所有显示窗口
			if (priority != 0) resortAllWindows();	//重新排序所有的窗口
			if(isClosing && showNow == false) return;
			if (_aWindowsQueue.length > 0) 
			{
				var _showWindow:Windows = _aWindowsQueue[0];
								
				if (!_showWindow.bVisible || showNow) {
					if (!_mBackGround.visible)_mBackGround.visible = _showWindow.bgVisible;
					_showWindow.appear();
					if(pause) FrameDrive.pause();
					_showWindow.visible = true;
					if (showNow && showBg) {	//如果是立刻显示调整弹窗及遮罩的层级
						_mMain.setChildIndex(_mBackGround, _mMain.numChildren - 1);
						_mMain.setChildIndex(_showWindow, _mMain.numChildren - 1);
					}
				}
			}
		}
		/**
		 * 显示加载好友家的弹窗
		 * @param	win
		 */
		public function showLoadProgressbar(name:String ="", clear:Boolean = true):void {
			if(clear){
				for (var i:int = 0; i < _aWindowsQueue.length; i++ ) {
					if (_aWindowsQueue[i] && _mMain.contains(_aWindowsQueue[i]))
						_mMain.removeChild(_aWindowsQueue[i]);
				}
				_aWindowsQueue.length = 0;
			}
			
//			if (!smallLoadPanel) smallLoadPanel = new SmallLoadAlert();
//			smallLoadPanel.showName(name);
//			showWindow(smallLoadPanel, true);
		}
		/**
		 * 隐藏加载好友家的弹窗
		 */
		public function hideLoadProgressbar():void {
//			if (!smallLoadPanel) return;
//			hideAppointWindow(smallLoadPanel);
		}
		/**
		 * 重新排序所有的窗口
		 */
		private function resortAllWindows():void 
		{
			backAllWindows();
			SortVector.sProperty = "priority";
			_aWindowsQueue.sort(SortVector.moreLess);
		}
		/**
		 * 回退所有显示窗口
		 */
		public function backAllWindows():void {
			var len:int = _aWindowsQueue.length;
			for (var i:int = 0; i < len; i += 1 ) {
				_aWindowsQueue[i].visible = false;
				_aWindowsQueue[i].bVisible = false;
			}
			_mBackGround.visible = false;
			try
			{
//				if(_controller.scene.isoWorld.status==SceneConfig.SCENE_NORMAL)
//					FrameDrive.play();
			} catch(error:Error) { }
		}
		/**
		 * 添加到舞台并设置坐标
		 * @param	win
		 */
		private function setPoint(win:Windows, bl:Boolean = true):void
		{
			if (bl)_mMain.addChild(win);
			_mMain.x = 0;
			_mMain.y = 0;
			win.x = int(Alignment.CalcXRegedValue(win.width , win.align , Alignment.CalcXAlignedValue(App.appstage.stageWidth,  win.align)) + win.popOffsetX);//_iCenterX;
			win.y = int(Alignment.CalcYRegedValue(win.height, win.align , Alignment.CalcYAlignedValue(App.appstage.stageHeight,  win.align)) + win.popOffsetY);//_iCenterY;
			if (!_diction[win])_diction[win] = win;
		}
		/**
		 * 获取窗体的显示状态(显示或者隐藏)
		 * @param	value
		 * @return
		 */
		public function getWindowVisible(value:Windows):Boolean
		{
			if (!_diction[value]) return false;
			return (_diction[value] as Windows).bVisible;
		}
		/**
		 * 判断窗体是否在队列中<显示中|排队中>
		 * @param value 窗体
		 */		
		public function isInQueue(value:Windows):Boolean
		{
			return _aWindowsQueue.indexOf(value) > -1;
		}
		/**
		 * 销毁窗体方法 调用自身清空引用 然后再删除本类中的引用
		 * @param	value
		 */
		public function disposeWindow(value:Windows):void
		{
			hideAppointWindow(value, true);
		}
		/**
		 * 窗口大小改变时，绘制弹窗背景
		 */
		private function drawBackGround(...args):void 
		{
			_mBackGround.graphics.clear();
			_mBackGround.graphics.beginFill(0, .7);
			_mBackGround.graphics.drawRect(0, 0, App.appstage.stageWidth, App.appstage.stageHeight);
			_mBackGround.graphics.endFill();
			/* 若当前有面板在显示，则调整位置   */
			if (_aWindowsQueue && _aWindowsQueue.length > 0) {
				for (var i:int = 0; i < _aWindowsQueue.length; i += 1 ) {
					setPoint(_aWindowsQueue[i], false);
				}
			}
		}
		
		/*隐藏指定的窗口*/
		public function hideAppointWindow(value:Windows, destroy:Boolean = false):void
		{
			isClosing = false;
			if (!_diction[value]) return;
			if(_mMain.contains(value))_mMain.removeChild(value);
			//从队列中删除此窗体
			var index:int = _aWindowsQueue.indexOf(value);
			if (index != -1)_aWindowsQueue.splice(index, 1);
			delete _diction[value];
			//删除后如果队列为0 隐藏遮罩层 否则显示队列的首位元素并调整层级关系
			if (_aWindowsQueue.length == 0) {
				try{
					//yuri 不管什么状态只要没有弹框都要显示
//					_controller.dispatch(new Command(CommandNameConst.COMMAND_SHOW_FRIENDS_VIEW));
//					if(_controller.scene.isoWorld.status==SceneConfig.SCENE_NORMAL){
//						FrameDrive.play();
//					}
				}catch(error:Error) { }
				_mBackGround.visible = false;
			}
			else {
				_mBackGround.visible = _aWindowsQueue[0].bgVisible;
				if (!_aWindowsQueue[0].bVisible) {
					setPoint(_aWindowsQueue[0]);
					_aWindowsQueue[0].visible = true;
					_aWindowsQueue[0].appear();
				}
				FrameDrive.pause();
				_mMain.setChildIndex(_mBackGround, _mMain.numChildren - 1);
				_mMain.setChildIndex(_aWindowsQueue[0], _mMain.numChildren - 1);
			}
			value.dispose();
		}
		
		/* INTERFACE frame.interfaces.IWindowsPanel */
		/*插入窗体到队列 不会主动显示*/
		public function insertQueue(value:Windows):void 
		{
			//插入时如果队列长度为0 就直接显示
			if (_aWindowsQueue.length == 0) 
				showWindow(value);
			else {
				for (var i:int = 0; i < _aWindowsQueue.length; i++) { //查找队列中第一个隐藏窗体的位置
					if (!_aWindowsQueue[i].bVisible) break;
				}
				if (value)_aWindowsQueue.splice(i, 0, value);	//插入到队列 <最后一个正在显示的面板和第一个隐藏面板之间>
			}
		}
		/**
		 * 设置窗体容器
		 */
		public function set mMain(value:Sprite):void 
		{
			_mMain = value;
			init();
		}
		
	}
}